#ifndef USERAGENTGENERATOR_H
#define USERAGENTGENERATOR_H

#include "libs/core/model/modeltype.h"
#include <QHash>
#include <QRandomGenerator>
#include <QStringList>

class UserAgentGenerator
{
public:
    static QString getRandomUserAgent()
    {
        // // iOS User Agents
        // static const QStringList iosUserAgents = {
        //     "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML,
        //     " "like Gecko) Version/16.5 Mobile/15E148 Safari/604.1", "Mozilla/5.0 (iPhone; CPU
        //     iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, " "like Gecko)
        //     CriOS/112.0.5615.46 Mobile/15E148 Safari/604.1", "Mozilla/5.0 (iPhone; CPU iPhone OS
        //     16_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, " "like Gecko) Mobile/15E148",
        //     "Mozilla/5.0 (iPhone; CPU iPhone OS 15_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML,
        //     " "like Gecko) Version/15.6 Mobile/15E148 Safari/604.1", "Mozilla/5.0 (iPhone; CPU
        //     iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, " "like Gecko)
        //     Version/14.7.1 Mobile/15E148 Safari/604.1", "Mozilla/5.0 (iPhone; CPU iPhone OS
        //     13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, " "like Gecko) Version/13.0.3
        //     Mobile/15E148 Safari/604.1"
        // };

        // Android User Agents
        static const QStringList androidUserAgents = {
            "Mozilla/5.0 (Linux; Android 13; SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12; M2012K11AC) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 11; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; SM-A536B) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12; SM-A325F) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 11; SM-A515F) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 10; SM-A315G) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; OnePlus9 Build/RP1A.201005.001.O2106) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12; moto g(10) Build/S1SAS32.42-10-10; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 11; Redmi Note 9 Pro Build/RKQ1.200826.002; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 10; VOG-L29 Build/HUAWEIVOG-L29; wv) AppleWebKit/537.36 "
            "(KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; Galaxy A54 5G Build/TQ3A.230601.015; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12L; Pixel 6 Build/SP2A.220405.004; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 11; Mi 11 Lite Build/RKQ1.200826.002; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 10; ROG Phone II Build/QKQ1.190825.002.ASUS; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; Find X3 Pro Build/RP1A.200720.011.OPD3.210104.004; "
            "wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12; Xperia 1 III Build/61.0.A.11.111; wv) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/118.0.0.0 Mobile "
            "Safari/537.36",
            "Mozilla/5.0 (Linux; Android 14; Galaxy Z Fold5) AppleWebKit/537.36 (KHTML, like "
            "Gecko) Chrome/123.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; OnePlus 11R) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12; Mi 12T) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/121.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 11; ROG Phone 5) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/120.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 10; Xperia 5 II) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/119.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; Pixel 8 Pro) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/122.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 12L; Samsung Galaxy Tab S8+) AppleWebKit/537.36 (KHTML, "
            "like Gecko) Chrome/121.0.0.0 Safari/537.36",
            "Mozilla/5.0 (Linux; Android 11; Huawei P40 Lite) AppleWebKit/537.36 (KHTML, like "
            "Gecko) Chrome/120.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 10; LG V60 ThinQ) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/119.0.0.0 Mobile Safari/537.36",
            "Mozilla/5.0 (Linux; Android 13; ASUS ROG Phone 6D Ultimate) AppleWebKit/537.36 "
            "(KHTML, like Gecko) Chrome/122.0.0.0 Mobile Safari/537.36"
        };

        // // 随机选择 iOS 或 Android
        // const QStringList& userAgents =
        //         (QRandomGenerator::global()->bounded(2) == 0) ? iosUserAgents :
        //         androidUserAgents;

        return androidUserAgents[QRandomGenerator::global()->bounded(androidUserAgents.size())];
    }

    static QString getUserAgent(const QString& key, int type)
    {
        if (type == yc::ANDROID_USER_AGENT_TYPE) {
            static QHash<QString, QString> cache;
            const QString id = key + ',' + QString::number(type);
            QString ua = cache.value(id);
            if (ua.isEmpty()) {
                const auto values = cache.values();
                ua = getRandomUserAgent();
                while (values.contains(ua)) {
                    ua = getRandomUserAgent();
                }
                cache.insert(id, ua);
            }
            return ua;
        } else {
            return QString("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, "
                           "like Gecko) QtWebEngine/6.8.3 Chrome/122.0.0.0 Safari/537.36");
        }
    }
};

#endif // USERAGENTGENERATOR_H
